package com.uls.found.service.impl;

import java.util.List;

import com.uls.common.exception.ServiceException;
import com.uls.common.utils.DateUtils;
import com.uls.common.utils.StringUtils;
import com.uls.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uls.found.mapper.EduRecordRecruitmentMapper;
import com.uls.found.domain.EduRecordRecruitment;
import com.uls.found.service.IEduRecordRecruitmentService;

/**
 * 招领记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-30
 */
@Service
public class EduRecordRecruitmentServiceImpl implements IEduRecordRecruitmentService 
{

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);


    @Autowired
    private EduRecordRecruitmentMapper eduRecordRecruitmentMapper;

    /**
     * 查询招领记录
     * 
     * @param foundId 招领记录主键
     * @return 招领记录
     */
    @Override
    public EduRecordRecruitment selectEduRecordRecruitmentByFoundId(Long foundId)
    {
        return eduRecordRecruitmentMapper.selectEduRecordRecruitmentByFoundId(foundId);
    }

    /**
     * 查询招领记录列表
     * 
     * @param eduRecordRecruitment 招领记录
     * @return 招领记录
     */
    @Override
    public List<EduRecordRecruitment> selectEduRecordRecruitmentList(EduRecordRecruitment eduRecordRecruitment)
    {
        return eduRecordRecruitmentMapper.selectEduRecordRecruitmentList(eduRecordRecruitment);
    }

    /**
     * 新增招领记录
     * 
     * @param eduRecordRecruitment 招领记录
     * @return 结果
     */
    @Override
    public int insertEduRecordRecruitment(EduRecordRecruitment eduRecordRecruitment)
    {
        eduRecordRecruitment.setCreateTime(DateUtils.getNowDate());
        return eduRecordRecruitmentMapper.insertEduRecordRecruitment(eduRecordRecruitment);
    }

    /**
     * 修改招领记录
     * 
     * @param eduRecordRecruitment 招领记录
     * @return 结果
     */
    @Override
    public int updateEduRecordRecruitment(EduRecordRecruitment eduRecordRecruitment)
    {
        eduRecordRecruitment.setUpdateTime(DateUtils.getNowDate());
        return eduRecordRecruitmentMapper.updateEduRecordRecruitment(eduRecordRecruitment);
    }

    /**
     * 批量删除招领记录
     * 
     * @param foundIds 需要删除的招领记录主键
     * @return 结果
     */
    @Override
    public int deleteEduRecordRecruitmentByFoundIds(Long[] foundIds)
    {
        return eduRecordRecruitmentMapper.deleteEduRecordRecruitmentByFoundIds(foundIds);
    }

    /**
     * 删除招领记录信息
     * 
     * @param foundId 招领记录主键
     * @return 结果
     */
    @Override
    public int deleteEduRecordRecruitmentByFoundId(Long foundId)
    {
        return eduRecordRecruitmentMapper.deleteEduRecordRecruitmentByFoundId(foundId);
    }

    @Override
    public String importUser(List<EduRecordRecruitment> eduRecordRecruitmentList, boolean updateSupport) {

        if (StringUtils.isNull(eduRecordRecruitmentList) || eduRecordRecruitmentList.size() == 0)
        {
            throw new ServiceException("导入客户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for(EduRecordRecruitment eduRecordRecruitment:eduRecordRecruitmentList){
            try {

                eduRecordRecruitmentMapper.insertEduRecordRecruitment(eduRecordRecruitment);
                successNum++;
                successMsg.append("<br/>" + successNum + "、招领名称： " +eduRecordRecruitment.getFoundName() + " 导入成功");
            } catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、招领名称： " +eduRecordRecruitment.getFoundName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }
}
